/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bd1.obli2012.gui;

import bd1.obli2012.framework.ExecutionResult;
import bd1.obli2012.framework.TablaManager;
import bd1.obli2012.framework.definicion.ForeignKey;
import bd1.obli2012.framework.definicion.Tabla;
import bd1.obli2012.gui.backend.Contexto;
import bd1.obli2012.versionado.Cambio;
import bd1.obli2012.versionado.TipoCambio;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JOptionPane;

/**
 *
 * @author favio.ortelli/guillermo.nasi
 */
public class PanelFKTabla extends javax.swing.JPanel {
    private final ForeignKey fk;
    private final DialogFKTabla dialogParent;
    private final Tabla tabla;
    

    
    
    /**
     * Creates new form PanelFKTabla
     */
    public PanelFKTabla(DialogFKTabla dialogparent,Tabla tabla, ForeignKey fk) {
        initComponents();
        this.fk = fk;
        this.dialogParent = dialogparent;
        this.tabla = tabla;
        this.lblColumna.setText(fk.getNombreColumna());
        this.lblFk.setText(fk.getFkConstraintName());
        this.lblColRef.setText(fk.getReferenciaColumna());
        this.lblTablaRef.setText(fk.getReferenciaTabla());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        lblColumna = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        lblFk = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        lblTablaRef = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        lblColRef = new javax.swing.JLabel();
        btnBorrar = new javax.swing.JButton();

        jLabel1.setFont(new java.awt.Font("Cantarell", 1, 12)); // NOI18N
        jLabel1.setText("Columna:");

        lblColumna.setFont(new java.awt.Font("Cantarell", 0, 12)); // NOI18N

        jLabel2.setFont(new java.awt.Font("Cantarell", 1, 12)); // NOI18N
        jLabel2.setText("FK:");

        lblFk.setFont(new java.awt.Font("Cantarell", 0, 12)); // NOI18N

        jLabel3.setFont(new java.awt.Font("Cantarell", 1, 12)); // NOI18N
        jLabel3.setText("Tabla Ref.: ");

        lblTablaRef.setFont(new java.awt.Font("Cantarell", 0, 12)); // NOI18N

        jLabel4.setFont(new java.awt.Font("Cantarell", 1, 12)); // NOI18N
        jLabel4.setText("Col. Ref.: ");

        lblColRef.setFont(new java.awt.Font("Cantarell", 0, 12)); // NOI18N

        btnBorrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/bd1/obli2012/icons/buttons/delete22.png"))); // NOI18N
        btnBorrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBorrarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblColumna, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblFk, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblTablaRef, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblColRef, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnBorrar))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel1)
                .addComponent(lblColumna, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel2)
                .addComponent(lblFk, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel3)
                .addComponent(lblTablaRef, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel4)
                .addComponent(lblColRef, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addComponent(btnBorrar)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnBorrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBorrarActionPerformed
       TablaManager
                tm = new TablaManager();
       ExecutionResult er =  tm.dropConstraint(this.tabla, this.fk.getFkConstraintName());
       if (er.success) {
            MainFrame.getInstance().getPanelTabla().actualizarDatos();
            
            
            Map<String, String> parametros = new HashMap<String, String>();
           
            parametros.put("NOMBRE_TABLA", this.tabla.getNombre());
            parametros.put("NOMBRE_CONSTRIAINT", this.fk.getFkConstraintName());
            parametros.put("NOMBRE_COLUMNA", this.fk.getNombreColumna());
            parametros.put("TABLA_REFERENCIA", this.fk.getReferenciaTabla());
            parametros.put("COLUMNA_REFERENCIA", this.fk.getReferenciaColumna());

            Cambio cambio = new Cambio(TipoCambio.TABLA_DROP_FK, parametros);
            Contexto.getInstance().guardarCambio(cambio);
            this.dialogParent.construirVista();
       } else {
            JOptionPane.showMessageDialog(null, er.errorMessage, "Error", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_btnBorrarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBorrar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel lblColRef;
    private javax.swing.JLabel lblColumna;
    private javax.swing.JLabel lblFk;
    private javax.swing.JLabel lblTablaRef;
    // End of variables declaration//GEN-END:variables
}
